<?php


function form_set_download($pr_yearly_income_id = 0){
	$view  = form_header( "..." , "..."  );
	$query_check_periode = " SELECT * FROM pr_yearly_income a
			INNER JOIN yearly_income b 
				ON a.yearly_income_id = b.yearly_income_id
			ORDER BY pr_yearly_income_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['pr_yearly_income_id']] = $prow['label'] .' '. $prow['tahun'] ;
	}
	$fperiode_id = array(
		'id'=>'pr_yearly_income_id',
		'value'=>$pr_yearly_income_id ,
		'name'=>'pr_yearly_income_id',
	);
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Yearly income"   );
	
		$submit = array(
		'value' => (  ' Download '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display(  $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( ); 
	return $view;
}
 
function report_pr_komponen_yearly_income( $pr_yearly_income_id){

	$header = array(
		'NIK'=>array(),  
		'CCC'=>array(),  
		'Account'=>array(),  
		'Description'=>array(), 
		'Nominal'=>array(),   
	);
	
	$query = "
	SELECT 
		b.nik,
		a.pr_yearly_income_id,
		a.komponen_id, 
		a.karyawan_id, 
		a.nominal 	
	from pr_yearly_income_karyawan a
	INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
	WHERE   a.pr_yearly_income_id = {$pr_yearly_income_id} ORDER BY b.nik ASC ";
  
	$result = my_query($query);
	$row = array();
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++; 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan 	= loaddata_jabatan($karyawan['jabatan_id']);
		$ccc = my_get_data_by_id('pos_biaya' ,'pos_biaya_id' , $jabatan['pos_biaya_id']);
		
		$nominal = ($ey['nominal'] < 0) ? $ey['nominal'] * (-1) : $ey['nominal'] ;
		if($nominal == 0)continue;
		
		$komponen 	= my_get_data_by_id('pr_yearly_income_komponen' , 'komponen_id', $ey['komponen_id'] );
		if($komponen['tipe'] <> 'Detil')continue;
		
		$account 	= get_jurnal_by_komponen_yearly_income( $ey['komponen_id']  ,$pr_yearly_income_id );
		 
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),     
			'ccc'=>  $ccc['code'],    
			'acount'=>  $account ,      
			'nama'=> $komponen['label'],
			'baru'=>  $nominal 
		); 
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,3 ,false ); 	
}

function get_jurnal_by_komponen_yearly_income($komponen_id , $pr_yearly_income_id){
	$query = "
		SELECT b.kode_jurnal_gaji FROM pr_yearly_income_komponen a
			INNER JOIN jurnal_gaji b ON a.jurnal_gaji_id = b.jurnal_gaji_id
		WHERE komponen_id = {$komponen_id} AND pr_yearly_income_id = {$pr_yearly_income_id} ";
	 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['kode_jurnal_gaji'];
}